import { observable, action, toJS } from 'mobx';

import { message, Modal } from 'antd';
/*
**原action页面的引用
*/
import * as API_LIST from '../apis/myVoting';
import objectAssign from 'object-assign';
import moment from 'moment';
import { WeaForm } from 'comsMobx';

import { WeaTableNew } from 'comsMobx';

const { TableStore } = WeaTableNew;

export default class MyVoting {
	title = '参与调查';
	@observable userList = [];

	// @observable comsWeaTableStore = new TableStore();
	@observable comsWeaTableStore = {
		basic: new TableStore(),

	};

  @observable form = {
    basic: new WeaForm(),
  }

	@observable status = {
		loading: false,
		showSearchAd: {
			basic: false,
		},
		allSessionkey: {
			basic: '',
		},
		conditions: {
			basic: '',
		},
		rightMenuDatas: [],

		selectedGroup: '',
		groupinfo: [],

		customDialog: false,

		tabkey: '0',
	};

	@action
	addUser(params) {
		this.userList.push({ name: 'dyf' });
	}

	getUserList(params) {
		return this.userList.slice();
	}


	doLoading = (loading) => {
		this.status = { ...this.status, loading };
	}

  @action
	doInit = (callback) => {
		API_LIST.tabInfo({}).then(action((result) => {
      let selectedGroup = 1000;
      let groupinfo = result.groupinfo;
      result.groupinfo.forEach((record) => {
        if (record.selected) {
          selectedGroup = record.viewcondition;
        }
      });
      if (selectedGroup != 1000) {
        let p3 = API_LIST.rightMenu({ listType: 'myVoting', viewcondition: selectedGroup });
        let p4 = API_LIST.myVoting({ viewcondition: selectedGroup });
        Promise.all([p3, p4]).then((result) => {
          this.comsWeaTableStore.basic.getDatas(result[1].sessionkey, 1);
          let allSessionkey = { ...this.status.allSessionkey, basic: result[1].sessionkey };
          this.status = {
            ...this.status,
            selectedGroup,
            groupinfo,
            allSessionkey,
          };
        });
      } else {
        this.status = {
          ...this.status,
          selectedGroup,
          groupinfo,
        };
      }

      this.getConditionInfo({ listType: 'myVoting', viewcondition: '0' });
      this.getRightMenu({ listType: 'myVoting', viewcondition: '0' });
    }));
	}


	changeTabKey = (key) => {
		this.status = {
			...this.status,
			selectedGroup: key,
			loading: true,
      tabkey: key,
		};

		/* API_LIST.myVoting({ viewcondition: key }).then((result) => {
			this.comsWeaTableStore.basic.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, 'basic': result.sessionkey }
			this.status = {
				...this.status,
				allSessionkey: allSessionkey,
				loading: false,
			}
		}) */

		this.doSearch({ listType: 'myVoting', viewcondition: key });
    this.getConditionInfo({ listType: 'myVoting', viewcondition: key });
    this.getRightMenu({ listType: 'myVoting', viewcondition: key });
	}

  @action
  doSearch = (param) => {
    API_LIST.myVoting({ ...param }).then(action((result) => {
      this.comsWeaTableStore.basic.getDatas(result.sessionkey, 1);
      let allSessionkey = { ...this.status.allSessionkey, basic: result.sessionkey };
      this.status = {
        ...this.status,
        allSessionkey,
        loading: false,
      };
      this.setShowSearchAd(false, 'basic');
    }));
  }


	// 高级搜索
  @action
	getConditionInfo = (params) => {
    API_LIST.condition(params).then(action((result) => {
      this.form.basic.initFormFields(result.condition);

      this.status = {
        ...this.status,
        conditions: { basic: result.condition },
      };
    }));
	}

	// 右键菜单
  @action
  getRightMenu = (params) => {
    API_LIST.rightMenu(params).then(action((result) => {
      this.status = {
        ...this.status,
        rightMenuDatas: result.rightMenus,
      };
    }));
  }

  @action
	getCalendarList = (value = {}) => { // 处理多个table的方法
		this.doLoading(true);
		API_LIST.getCalendarListP(value).then(action((data) => {
      this.comsWeaTableStore = { ...this.comsWeaTableStore, [data.sessionkey]: new TableStore() };
      this.comsWeaTableStore[data.sessionkey].getDatas(data.sessionkey, 1);
      this.status = { ...this.status, dataKey: data.sessionkey, loading: false };
    }));
	}

	setShowSearchAd = (bool, type = 'basic') => {
		let showSearchAd = { ...this.status.showSearchAd, [type]: bool };
		this.status = { ...this.status, showSearchAd };
	}

	setCustomDialog = (bool, joinType = '', votingDialogId = '') => {
		this.status = {
			...this.status,
			customDialog: bool,
			votingDialogId,
			joinType,
		};
	}
}
